package de.gwtprime.client.widgets.trading.popup;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.event.OrderProcessedEvent;
import de.gwtprime.client.widgets.trading.OrderOverviewWidget;
import de.gwtprime.shared.requestFactory.OrderProxy;

public class OrderPopupStorno extends PopupPanel {

	private static OrderPopupStornoUiBinder uiBinder = GWT
			.create(OrderPopupStornoUiBinder.class);

	interface OrderPopupStornoUiBinder extends
			UiBinder<Widget, OrderPopupStorno> {
	}

	@UiField SimplePanel orderOverview;
	@UiField Button stornoBtn;
	
	public OrderPopupStorno(final ClientFactory clientFactory, final OrderProxy order) {
		super(true, true); // auto hide and modal
		setWidget(uiBinder.createAndBindUi(this));
		
		orderOverview.add(new OrderOverviewWidget(clientFactory, order));
		
		
		stornoBtn.setText("Order stornieren");
		stornoBtn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				clientFactory.getRequestFactory().orderRequest().stornoTheOrder().using(order).fire(new Receiver<Void>() {

					@Override
					public void onSuccess(Void response) {
						clientFactory.getNotificationCenter().showMessage("Order wurde storniert");
						clientFactory.getEventBus().fireEvent(new OrderProcessedEvent(order));
					}
				});
				
			}
		});
	}

}
